package com.iretailer.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iretailer.base.BaseController;
import com.iretailer.model.*;
import com.iretailer.util.ResponeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@RestController
public class SiteDeviceController extends BaseController {

    @Value("${TimeDiff}")
    private Integer timeDiff;

    @PostMapping(value = "/selectNoteData.action")
    public void selectNoteData(HttpServletResponse response, HttpServletRequest request, @RequestBody Map<String, Object> map) {
        String siteId = (String) map.get("siteId");
        String startTime = (String) map.get("startTime");
        String endTime = (String) map.get("endTime");
        int pageIndex = (int) map.get("pageIndex");
        int pagetSize = (int) map.get("pageSize");
        IPage<ItemNote> result = notesService.selectNotePage(new Page<ItemNote>(pageIndex, pagetSize), siteId,startTime,endTime);
        ResponeUtil.sendSuccess(response, result);

    }

    @GetMapping(value = "/selectSite.action")
    public void selectSite(HttpServletResponse response, HttpServletRequest request) {
        List<Site> sites = siteService.list(new QueryWrapper<Site>().lambda().eq(Site::getSiteStatus, "1"));
        System.out.println(JSON.toJSONString(sites));
        ResponeUtil.sendSuccess(response, sites);
    }

    @PostMapping(value = "/selectDevice")
    public void selectDevice(HttpServletResponse response, HttpServletRequest request, @RequestBody Device device) {
        List<Device> devices = deviceService.list(new QueryWrapper<Device>().lambda()
                .eq(Device::getSiteId, device.getSiteId())
                .eq(Device::getDeviceStatus, device.getDeviceStatus()));
        ResponeUtil.sendSuccess(response, devices);
    }

    @PostMapping(value = "/allSiteDevice.action")
    public void selectAllSiteDevice(HttpServletResponse response, HttpServletRequest request, @RequestBody Map<String, Object> map) {

        List<SiteDevice> findSiteDevice = deviceService.selectAllSiteDevice(1, 1);

        Map<String, MapSiteDevice> mapSiteDeviceMap = new HashMap<>();
        Map<String, Object> result = new HashMap<>();

        AtomicReference<Integer> deviceOnline = new AtomicReference<>(0);
        findSiteDevice.forEach((it) -> {
            if (it.getStatus() <= timeDiff) {
                deviceOnline.getAndSet(deviceOnline.get() + 1);
            }
        });
        result.put("allDevice", findSiteDevice.size());
        result.put("deviceOnline", deviceOnline.get());

        //所有物品
        List<SiteItem> siteItems = itemService.selectAllSiteItem(1);
        if (siteItems != null && siteItems.size() > 0) {
            AtomicReference<Integer> itemOnline = new AtomicReference<>(0);
            siteItems.forEach((it) -> {
                MapSiteDevice find = mapSiteDeviceMap.get(it.getSiteName());
                if (find == null) {
                    find = new MapSiteDevice(it.getSiteName(), it.getLongitude(), it.getLatitude(), 0, 0, new ArrayList<SiteItem>());
                }
                jugeOnline(find, it, itemOnline);
                mapSiteDeviceMap.put(it.getSiteName(), find);
            });
            result.put("allItem",siteItems.size());
            result.put("itemOnline",itemOnline.get());
        }

        result.put("siteItem", mapSiteDeviceMap.values());
        ResponeUtil.sendSuccess(response, result);

    }

    private void jugeOnline(MapSiteDevice msd, SiteItem si, AtomicReference<Integer> itemOnline) {
        msd.setTotal(msd.getTotal() + 1);
        si.setOnline(-1);
        if (si.getStatus() <= timeDiff) {
            msd.setOnline(msd.getOnline() + 1);
            itemOnline.getAndSet(itemOnline.get() + 1);
            si.setOnline(1);
        }
        msd.getItems().add(si);
    }
}
